#ifndef __MAIZE_RTOS_LIST_H
#define __MAIZE_RTOS_LIST_H

#include "mzrtos.h"

#ifdef __cplusplus
extern "C" {
#endif

#define mz_container_of(ptr, type, member) \
    ((type *)((char *)(ptr) - (unsigned long)(&((type *)0)->member)))

#define mz_list_entry(ptr, type, member) \
    mz_container_of(ptr, type, member)

mz_inline void mz_list_init(mz_list_t l){
    l->prev = l->next = l;
}

mz_inline void mz_list_insert_after(mz_list_t l, mz_list_t n){
    l->next->prev = n;
    n->next = l->next;
    n->prev = l;
    l->next = n;
}

mz_inline void mz_list_insert_before(mz_list_t l, mz_list_t n){
    l->prev->next = n;
    n->prev = l->prev;
    n->next = l;
    l->prev = n;
}

mz_inline void mz_list_remove(mz_list_t l){
    l->next->prev = l->prev;
    l->prev->next = l->next;

    mz_list_init(l);
}

#ifdef __cplusplus
}
#endif

#endif